// Decompiled with JetBrains decompiler
// Type: FibMatrix.Config.IntIdMetaSet
// Assembly: FMCommon, Version=1.0.8663.21262, Culture=neutral, PublicKeyToken=null
// MVID: 52773074-FB09-4BA0-8FA9-4782D11D6C17
// Assembly location: /Users/mac/work/RiverGame/com.rivergame.fmframework/Runtime/Plugins/FMCommon.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace FibMatrix.Config
{
  public class IntIdMetaSet : MetaItemSet, IMetaItemSetImpl<int>, IMetaItemSet, IEnumerator<IMetaKeyValueCollection>, IDisposable, IEnumerator
  {
    private Dictionary<int, int> _idRowIdxMap;

    public IntIdMetaSet(string name)
      : base(name)
    {
    }

    public int[] idList
    {
      get
      {
        if (this._idRowIdxMap == null)
          return (int[]) null;
        int[] array = new int[this._idRowIdxMap.Count];
        this._idRowIdxMap.Keys.CopyTo(array, 0);
        return array;
      }
    }

    public bool HasMetaProps(int id)
    {
      if (this._idRowIdxMap == null)
        throw new Exception("请在delayMode下调用");
      return this._idRowIdxMap.ContainsKey(id);
    }

    public IMetaKeyValueCollection GetMetaPropsById(int id)
    {
      if (this._idRowIdxMap == null)
        throw new Exception("请使用delayMode获取此对象");
      int row;
      if (!this._idRowIdxMap.TryGetValue(id, out row))
        return (IMetaKeyValueCollection) null;
      this._helper.MoveToRow(row);
      this._kvMap.FillRowVals();
      return (IMetaKeyValueCollection) this._kvMap;
    }

    protected override unsafe void CalculateIdRowIndexMap()
    {
      if (!this._helper.hasId)
        throw new InvalidDataException(this._name + " doesn't has id field, unsupport delay mode!");
      int totalCount = this.totalCount;
      this._idRowIdxMap = new Dictionary<int, int>(totalCount);
      int currentRow = this._helper.currentRow;
      fixed (byte* bufferPtr = this._helper.buffer)
      {
        for (int row = 0; row < totalCount; ++row)
        {
          this._helper.MoveToRow(row);
          int val = -1;
          if (this._helper.GetFirstType(bufferPtr) != MetaSerializeType.Int)
            throw new Exception("id type not int");
          if (!this._helper.GetFirstPropVal(bufferPtr, MetaSerializeType.Int, out val))
            throw new Exception("id val byte len is 0");
          this._idRowIdxMap.Add(val, this._helper.currentRow);
        }
      }
      this._helper.MoveToRow(currentRow);
    }
  }
}
